package com.foreknow.service.impl;

import com.foreknow.bean.Guestbook;
import com.foreknow.dao.impl.AdminDaoImpl;
import com.foreknow.dao.impl.GuestbookDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.service.GuestbookService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GuestbookServiceImpl implements GuestbookService {
  @Override
  public List<Object> queryInfo() {
    //连接数据库
    DBManager dbManager = DBManager.getInstance();
    //调用连接数据库的方法
    Connection conn = dbManager.getConnection();
    //创建dao层的对象
    GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
    //调用AdminDaoImpl对象中的 public void setConnection(Connection connection)方法连接数据库
    guestbookDao.setConnection(conn);
    List<Object> list = null;
    try {
       list = guestbookDao.getQueryAll();
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return list;
  }

  @Override
  public boolean add(Guestbook guestbook) {
    DBManager dbManager = DBManager.getInstance();
    Connection conn = dbManager.getConnection();
    GuestbookDaoImpl guestbookDao=new GuestbookDaoImpl();
    guestbookDao.setConnection(conn);
    boolean isRight = false;
    try {
      isRight = guestbookDao.save(guestbook);
      conn.commit();
    } catch (SQLException e) {
      try {
        conn.rollback();
      } catch (SQLException ex) {
        ex.printStackTrace();
      }
      e.printStackTrace();
    }
    return isRight;
  }

  @Override
  public boolean del(Integer id) {
    DBManager dbManager = DBManager.getInstance();
    Connection conn = dbManager.getConnection();
    GuestbookDaoImpl guestbookDao=new GuestbookDaoImpl();
    guestbookDao.setConnection(conn);
    boolean isRight = false;
    try {
      isRight = guestbookDao.delete(id);
      conn.commit();
    } catch (SQLException e) {
      try {
        conn.rollback();
      } catch (SQLException ex) {
        ex.printStackTrace();
      }
      e.printStackTrace();
    }
    return isRight;
  }

  @Override
  public boolean update(Guestbook guestbook) {
    DBManager dbManager = DBManager.getInstance();
    Connection conn = dbManager.getConnection();
    GuestbookDaoImpl guestbookDao=new GuestbookDaoImpl();
    guestbookDao.setConnection(conn);
    boolean isRight = false;
    try {
      isRight = guestbookDao.update(guestbook);
      conn.commit();
    } catch (SQLException e) {
      try {
        conn.rollback();
      } catch (SQLException ex) {
        ex.printStackTrace();
      }
      e.printStackTrace();
    }
    return isRight;
  }

  @Override
  public Guestbook queryById(Integer id) {
    //连接数据库
    DBManager dbManager = DBManager.getInstance();
    //调用连接数据库的方法
    Connection conn = dbManager.getConnection();
    //创建dao层的对象
    GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
    //调用AdminDaoImpl对象中的 public void setConnection(Connection connection)方法连接数据库
    guestbookDao.setConnection(conn);
    Guestbook guestbook=null;
    try {
      guestbook = guestbookDao.queryById(id);
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return guestbook;
  }

}
